package com.mayisoftware.mayioa.platform.impl.task;


import com.mayisoftware.mayioa.common.impl.utils.POIUtil;
import com.mayisoftware.mayioa.common.impl.utils.StringUtils;
import com.mayisoftware.mayioa.platform.api.domain.SysDept;
import com.mayisoftware.mayioa.platform.api.domain.SysDictData;
import com.mayisoftware.mayioa.platform.api.domain.SysPost;
import com.mayisoftware.mayioa.platform.api.domain.SysUser;
import com.mayisoftware.mayioa.platform.api.service.*;
import com.mayisoftware.mayioa.platform.impl.serviceImpl.SysConfigServiceImpl;
import com.mayisoftware.mayioa.platform.impl.serviceImpl.SysLogininforServiceImpl;
import com.mayisoftware.mayioa.platform.impl.serviceImpl.SysOperLogServiceImpl;
import com.mayisoftware.mayioa.platform.impl.shiro.service.SysPasswordService;
import com.mayisoftware.mayioa.platform.impl.util.ShiroUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.*;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 定时任务调度计划解析
 *
 *
 */
@Component("logCleanTask")
public class LogCleanTask
{
    /**
     * 日志对象
     */
    private static final Logger log = LoggerFactory.getLogger(LogCleanTask.class);

    /**
     * 日志默认保留期限
     */
    public static final int DEFAULT_RESERVATION_TIME = 6 ;

    /**
     * 日志默认保留期限
     */
    public static final String RESERVATION_TIME_KEY = "sys.log.reservationTime" ;

    /**
     * 系统访问日志服务
     */
    @Autowired
    ISysLogininforService sysLogininforService;

    /**
     * 操作日志记录服务
     */
    @Autowired
    ISysOperLogService sysOperLogService ;

    /**
     * 参数配服务
     */
    @Autowired
    ISysConfigService sysConfigService;

    /**
     * 任务日志服务
     */
    @Autowired
    ISysJobLogService sysJobLogService;


    public void cleanLog(){
        int reservationTime = DEFAULT_RESERVATION_TIME ;
        //查询配置的日志保留期限
        String reservation_time = sysConfigService.selectConfigByKey(RESERVATION_TIME_KEY);
        if(!StringUtils.isEmpty(reservation_time)){
            reservationTime = Integer.parseInt(reservation_time);
        }
        Calendar cal= Calendar.getInstance();
        cal.add(Calendar.MONTH,-reservationTime);
        Date reservationDate = cal.getTime();
        //清除日志
        sysLogininforService.cleanLogininforByTime(reservationDate);
        //清除操作日志
        sysOperLogService.cleanOperLogByTime(reservationDate);
        //清除任务日志
        sysJobLogService.cleanJobLogByTime(reservationDate);
    }


}
